package com.gs.synedocket.util;

import static com.gs.synedocket.constant.AllConstants.TAG;

import android.text.TextUtils;
import android.util.Log;

import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dao.LogUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class ParamInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        String url = request.url().toString();
        if (url.equals(OKHttpUtil.NET_URL + "/api/syns")) {  // 下载
            Headers headers = response.headers();
            syncDocketParam(headers);
            takePhotoParam(headers);
        } else if (url.equals(OKHttpUtil.NET_URL + "/api/syns/batchUploadDocket1")) { // 上传
            Headers headers = response.headers();
            uploadDocketParam(headers);
            takePhotoParam(headers);
        }
        return response;
    }

    private void uploadDocketParam(Headers headers) {
        String uploadDocketInterval = headers.get(AllConstants.UPLOAD_DOCKET_INTERVAL);
        Log.i(TAG, "ParamInterceptor: uploadDocketInterval：" + uploadDocketInterval);
        try {
            int interval;
            int defaultInterval = AppUtil.getInstance().getContext().getResources().getInteger(R.integer.upload_interval);
            if (!TextUtils.isEmpty(uploadDocketInterval) && (interval = Integer.parseInt(uploadDocketInterval)) != SPUtil.getInstance().getInt(AllConstants.UPLOAD_DOCKET_INTERVAL, defaultInterval)) {
                SPUtil.getInstance().putInt(AllConstants.UPLOAD_DOCKET_INTERVAL, interval);
                LogUtil.getInstance().insertOrReplaceData("收到服務器發送的同步上傳時間，修改同步下載時間為" + interval + "s");
            }
        } catch (NumberFormatException e) {
            Log.i(TAG, "ParamInterceptor: 参数异常，uploadDocketInterval:" + uploadDocketInterval);
        }
    }

    private void syncDocketParam(Headers headers) {
        String synDocketInterval = headers.get(AllConstants.SYNC_DOCKET_INTERVAL);
        Log.i(TAG, "ParamInterceptor: synDocketInterval：" + synDocketInterval);
        try {
            int interval;
            int defaultInterval = AppUtil.getInstance().getContext().getResources().getInteger(R.integer.download_interval);
            if (!TextUtils.isEmpty(synDocketInterval) && (interval = Integer.parseInt(synDocketInterval)) != SPUtil.getInstance().getInt(AllConstants.SYNC_DOCKET_INTERVAL, defaultInterval)) {
                SPUtil.getInstance().putInt(AllConstants.SYNC_DOCKET_INTERVAL, interval);
                LogUtil.getInstance().insertOrReplaceData("收到服務器發送的同步下載時間，修改同步下載時間為" + interval + "s");
            }
        } catch (NumberFormatException e) {
            Log.i(TAG, "ParamInterceptor: 参数异常，synDocketInterval：" + synDocketInterval);
        }
    }

    private void takePhotoParam(Headers headers) {
        String takePhoto = headers.get(AllConstants.TAKE_PHOTO);
        Log.i(TAG, "ParamInterceptor: takePhoto：" + takePhoto);
        int interval;
        int defaultInterval = AppUtil.getInstance().getContext().getResources().getInteger(R.integer.take_photo);
        if (!TextUtils.isEmpty(takePhoto) && (interval = Integer.parseInt(takePhoto)) != SPUtil.getInstance().getInt(AllConstants.TAKE_PHOTO, defaultInterval)) {
            SPUtil.getInstance().putInt(AllConstants.TAKE_PHOTO, interval);
            LogUtil.getInstance().insertOrReplaceData("收到服務器發送的是否拍照，修改是否拍照為" + interval);
        }
    }
}
